package com.zxx.casserver.modules.dictionary.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zxx.cascommon.base.exception.ParamsException;
import com.zxx.cascommon.base.result.Result;
import com.zxx.cascommon.base.result.ResultUtils;
import com.zxx.casserver.modules.dictionary.entity.DictionaryDO;
import com.zxx.casserver.modules.dictionary.entity.DictionaryVO;
import com.zxx.casserver.modules.dictionary.service.IDictionaryService;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 字典项服务控制层
 *
 * @author zhoukai
 * @date 2020/5/8 16:36
 */
@RestController
@RequestMapping("/dictionary")
public class DictionaryController {
    private final IDictionaryService dictionaryService;

    public DictionaryController(IDictionaryService dictionaryService) {
        this.dictionaryService = dictionaryService;
    }

    @PostMapping("/list")
    public Result list(@RequestBody DictionaryVO dictionaryVO) {
        IPage<DictionaryDO> dictionaryList = dictionaryService.search(dictionaryVO);
        return ResultUtils.success(dictionaryList);
    }


    @PostMapping("/save")
    public Result save(@RequestBody DictionaryVO dictionaryVO) {
        if (ObjectUtils.isEmpty(dictionaryVO.getType())) {
            throw new ParamsException("type is empty");
        }
        if (ObjectUtils.isEmpty(dictionaryVO.getLabel())) {
            throw new ParamsException("label is empty");
        }
        if (ObjectUtils.isEmpty(dictionaryVO.getValue())) {
            throw new ParamsException("value is empty");
        }

        DictionaryDO dictionary = dictionaryService.saveDictionary(dictionaryVO);
        if (ObjectUtils.isEmpty(dictionary)) {
            return ResultUtils.fail(dictionary);
        }
        return ResultUtils.success(dictionary);
    }

    @PostMapping("/update")
    public Result update(@RequestBody DictionaryVO dictionaryVO) {
        if (ObjectUtils.isEmpty(dictionaryVO.getDictId())) {
            throw new ParamsException("dictId is empty");
        }
        if (ObjectUtils.isEmpty(dictionaryVO.getType())) {
            throw new ParamsException("type is empty");
        }
        if (ObjectUtils.isEmpty(dictionaryVO.getLabel())) {
            throw new ParamsException("label is empty");
        }
        if (ObjectUtils.isEmpty(dictionaryVO.getValue())) {
            throw new ParamsException("value is empty");
        }
        DictionaryDO dictionary = dictionaryService.updateDictionary(dictionaryVO);
        if (ObjectUtils.isEmpty(dictionary)) {
            return ResultUtils.fail(dictionary);
        }
        return ResultUtils.success(dictionary);
    }


    @PostMapping("/delete")
    public Result delete(@RequestBody DictionaryVO dictionaryVO) {
        if (ObjectUtils.isEmpty(dictionaryVO.getIds())) {
            throw new ParamsException("ids is empty");
        }
        boolean success = dictionaryService.batchDelete(dictionaryVO);
        if (!success) {
            return ResultUtils.fail(dictionaryVO.getIds());
        }
        return ResultUtils.success(dictionaryVO.getIds());
    }

}
